package cn.yunhe.crm.controller;


import java.util.List;


import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.yunhe.crm.annotation.SystemLog;
import cn.yunhe.crm.entity.Organization;

import cn.yunhe.crm.service.OrganizationService;
@Scope("prototype")
@Controller
public class OrganizationController {
	@Autowired
	@Qualifier("OrganizationService")
	private OrganizationService organService;
	
	

	public void setOrganService(OrganizationService organService) {
		this.organService = organService;
	}
	
	
	/**
	 * 查看所有机构
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/infomanage.do")
	public String OrganizationList(HttpSession session){
		List<Organization> Organlist=organService.findAllOrganization();
		session.setAttribute("Organlist", Organlist);
	
		return "infomanage";
	}
	
	/**
	 * 跳转至添加页面
	 * @return
	 */
	@RequestMapping("infomanageadd.do")
	public String getinfomanageadd(){	
		return"infomanageadd";
	}
	
	/**
	 * 添加机构
	 * @param organization
	 * @return
	 */
	@SystemLog(des="添加机构")
	@RequestMapping("/addOrganization.do")
	public String infomanageadd(Organization organization){
		int flag=organService.insertOrganization(organization);
		if(flag>0){
			return "redirect:/infomanage.do";
		}else {
			return "infomanageadd";
		}
	}
	
	/**
	 * 跳转至修改页面
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping("/infomanageupdate.do")
	public String getinfomanageupdate(Integer id,HttpSession session){
		Organization organ=organService.getOrgById(id);
		session.setAttribute("organ", organ);
		return "infomanageupdate";
	}
	
	
	/**
	 * 编辑机构
	 * @param id
	 * @param organization
	 * @return
	 */
	@SystemLog(des="编辑机构")
	@RequestMapping("/udpateOrganization.do")
	public String infomanageupdate(Integer id,Organization organization){
		int flag=organService.updateOrganById(organization);
		if(flag>0){
			return "redirect:/infomanage.do";
		}else {
			return "null";
		}
		
	}
	
	
	/**
	 * 查看机构详情
	 * @param session
	 * @param id
	 * @return
	 */
	@RequestMapping("/infomanagedetail")
	public String infomanagedetail(HttpSession session,Integer id){
		Organization organ=organService.getOrgById(id);
		session.setAttribute("organ", organ);
		return "infomanagedetail";
	}
	
	/**
	 * 删除机构
	 * @param id
	 * @return
	 */
	@SystemLog(des="删除机构")
	@RequestMapping("/infodelbyid.do")
	public String delOrganById(Integer id){
		int flag=organService.editOrganStateById(id);
		if(flag>0){
			return "redirect:/infomanage.do";
		}
			return "null";
		
	}
	/**
	 * 机构名唯一性验证
	 * @param orgname
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/organization_validate.do")
	public String organizationValidate(String orgname){
		Organization organization=organService.researchOrganziation(orgname);
			if(organization !=null){
				return "true";
			}else {
				return "false";
			}
	}
}